package dd.server;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import dd.model.Event;
import dd.model.serverModel;

import server.ClientState;
import server.IProtocolHandler;
import xml.Message;

public class signInResponseController implements IProtocolHandler {

	@SuppressWarnings("null")
	public Message process(ClientState client, Message request) {
		serverModel sm = serverModel.getInstance();
		Event e = sm.getEvent();
		//Event e = new Event();
		Node update = request.contents.getFirstChild();
		NamedNodeMap map = update.getAttributes();
		String geteventid = map.getNamedItem("id").getNodeValue();
		System.out.println("receivedid:" + geteventid);
		String numberofline = String.valueOf(e.getNumberLines());
		String numberofedge = String.valueOf(e.getNumberEdges());

		// Construct message reflecting state
	/*	String xmlString = Message.responseHeader(request.id()) +
				 "<signInResponse id='"+geteventid+"' behavior='"+e.getType()+"' type='"
				+e.getMode()+"' question='"+e.getEventName()+"' numChoices='"
				 +e.getNumberLines()+"' numRounds='"+e.getNumberEdges()+"' position='2'>"+
	
		"</signInResponse></response>";*/
		String xmlString = Message.responseHeader(request.id()) +
		 "<signInResponse id='"+geteventid+"' behavior='"+e.getType()+"' type='"+e.getMode()+"' question='"+e.getEventName()+"' numChoices='"+numberofline+"' numRounds='"+numberofedge+"' position='1'>"+
		 "<choice value='"+e.getChoice(0)+"' index='0'/>"+
		 "<choice value='"+e.getChoice(1)+"' index='1'/>"+
		 "<choice value='"+e.getChoice(2)+"' index='2'/>"+
		 "<choice value='"+e.getChoice(3)+"' index='3'/>"+
		"</signInResponse></response>";
		//record this id to server id for event 135
		int i ;
		i = sm.getnumofuserin();
		sm.setuserid(i, request.id());
		sm.setnumofuserin(i+1);
		// send this response back to the client which sent us the request.
		return new Message (xmlString);
	}
}

